const TaskModel = require('../models/task.js');
let time = new Date().getTime();
let num = 0;
class TaskController {
    //增加任务
    static async createTask(ctx) {
        num = '' + (Number(num)+1);
        num = num.length === 4 ? num : num.length === 3 ? '0' + num : num.length === 2 ? '00' + num : '000' + num;
        console.log(num)
        const data = ctx.request.body;
        const date = new Date();
        let y = date.getFullYear();
        let m = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
        let d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
        data.id = '' + y + m + d + num;
        let addTask = await TaskModel.createTask(data)
        if (addTask) {
            ctx.body = {
                message: '任务添加成功',
                code: 0
            }
        } else {
            ctx.body = {
                message: '任务添加失败',
                code: 1
            }
        }
    }
    //删除任务
    static async deleteTask(ctx) {
        const data = ctx.request.body;
        let id = data._id
        let tasks = await TaskModel.deleteTask(id);
        if (tasks) {
            ctx.body = {
                message: '任务删除成功',
                code: 0
            }
        } else {
            ctx.body = {
                message: '任务删除失败',
                code: 1
            }
        }
    }
    //更新任务
    static async updateTask(ctx) {
        const data = ctx.request.body;
        let tasks = await TaskModel.updateTask(data);
        if (tasks) {
            ctx.body = {
                message: '任务更新成功',
                code: 0,
                data: articles
            }
        } else {
            ctx.body = {
                message: '任务更新失败',
                code: 1
            }
        }
    }
    //查询任务
    static async queryTask(ctx) {
        const data = ctx.request.body;
        let id = data.id ? data.id : undefined;
        let page = parseInt(data.pages || 1);
        let limit = parseInt(data.pageNum || 10);
        let skip = (page - 1) * limit;
        let searchData = {};
        let num = 0;
        let list = data.data;
        for (let i in list) {
            if (list[i] && list[i].length > 0) {
                list[i] = list[i].replace(/\//g, '\\/')
                if (i == 'tksubmittime' || i == 'tkenttime' || i == 'tkplantime') {
                    searchData[i] = { '$gte': list[i][0], '$lte': list[i][1] };
                } else {
                    searchData[i] = eval('/' + list[i] + '/');
                }
            }
        }
        console.log(searchData)
        let tasks = await TaskModel.queryTasks(id, limit, skip, searchData);

        if (tasks) {
            console.log(tasks)
            ctx.body = {
                message: '任务查询成功',
                data: tasks,
                code: 0
            }
        } else {
            ctx.body = {
                message: '任务查询失败',
                code: 1
            }
        }
    }
}
module.exports = TaskController